package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 插入套餐和套餐菜品关系
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
//1.插入套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);
//2.插入套餐菜品表

        List<SetmealDish> setmealDish = setmealDTO.getSetmealDishes();

       /* if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });*/
        //给dishid赋值
        if (setmealDish != null && setmealDish.size() > 0) {
            setmealDish.forEach(sd -> {
                sd.setSetmealId(setmeal.getId());
            });
            //批量插入套餐菜品关系表
            setMealDishMapper.insertSetmealDish(setmealDish);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        PageResult pageResult = new PageResult(total,records);
        return pageResult ;
    }

    @Override
    public SetmealVO getById(Long id) {
        //1.查询套餐表
       SetmealVO setmealVO= setmealMapper.getByIdsetmeal(id);
        //2.根据套餐id查询Dish_id查询菜品表

        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealIds(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //1.修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //2.修改菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //先删除菜品
        setMealDishMapper.delete(setmeal.getId());
        //查询列表是否存在
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });

        }
        //统一添加菜品
        setMealDishMapper.insertSetmealDish(setmealDishes);
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    public void updeteStatus(Integer status, Long id) {
        if (status == StatusConstant.ENABLE){
        List<Integer> stutusList = dishMapper.selectDishStatus(id);
        stutusList.forEach(sl->
        {
            if (sl == StatusConstant.DISABLE ){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });}

setmealMapper.updeteStatus(status,id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //1.判断套餐是否在起售中
        for (Long id : ids){
           Integer status = setmealMapper.selectsetmealStatus(id);
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //1.删除套餐
        setmealMapper.deleteBatchSetmeal(ids);
        //2.删除套餐关联菜品表
        setMealDishMapper.deleteBysetmealId(ids);
    }

    /**
     * 根据分类id查询套餐(起售中)
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
 List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据套餐id查询包含的菜品列表
     * @param id
     * @return
     */

    @Override
    public List<DishItemVO> getDishItemById(Long id) {

 List<DishItemVO> list= setMealDishMapper.getDishItemBySetmealId(id);
        return list;
    }


}